package cn.com.w.web.controller;

import cn.com.w.common.Emun.Register;
import cn.com.w.common.PO.LoginRequest;
import cn.com.w.common.VO.BaseVO;
import cn.com.w.minio.config.MinIOConfig;
import cn.com.w.minio.util.MinIOUtils;
import cn.com.w.web.Handler.AuthHandler;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Objects;

/**
 * 认证控制器，用于处理用户登录请求。
 *
 * @author w
 */
@RestController
@RequestMapping("/api/auth")
@Api(tags = "用户管理")
public class AuthController {

    private final AuthHandler authHandler;
    private final MinIOConfig minIOConfig;

    @Autowired
    public AuthController(AuthHandler authHandler, MinIOConfig minIOConfig) {
        this.authHandler = authHandler;
        this.minIOConfig = minIOConfig;
    }


    /**
     * 处理用户登录请求，验证用户信息并生成JWT令牌。
     *
     * @param loginRequest 包含用户名和密码的登录请求对象。
     * @return 包含JWT令牌的响应实体。
     */
    @PostMapping("/login")
    public BaseVO<?> authenticateUser(@RequestBody LoginRequest loginRequest) throws Exception {
        return authHandler.login(loginRequest);
    }

    @PostMapping("/logout")
    public BaseVO<?> authenticateUserOut(@RequestHeader(value = "Authorization") String token) throws Exception {
        return authHandler.logOut(token);
    }

    /**
     * 微信注册接口
     *
     * @param wechatId 微信唯一标识
     * @param username 用户名
     * @param password 密码
     * @return 注册结果
     */
    @PostMapping("/register/wechat")
    public BaseVO<?> registerByWechat(@RequestParam("wechatId") String wechatId,
                                      @RequestParam("username") String username,
                                      @RequestParam("password") String password) throws Exception {
        return authHandler.register(username, password, wechatId, Register.WECHAT);
    }

    @PostMapping("/upload")
    public String testUpload(MultipartFile file) throws Exception {
        String suffixName = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = LocalDateTime.now().atOffset(ZoneOffset.UTC).toInstant().toEpochMilli()  + suffixName;
        MinIOUtils.uploadFile(minIOConfig.getBucketName(), fileName, file.getInputStream());
        //返回图片链接
        return minIOConfig.getFileHost() + "/" + minIOConfig.getBucketName() + "/" + fileName;
    }

    /**
     * 手机号注册接口
     *
     * @param phoneNumber 手机号
     * @param username    用户名
     * @param password    密码
     * @return 注册结果
     */
    @PostMapping("/register/phone")
    public BaseVO<?> registerByPhone(@RequestParam("phoneNumber") String phoneNumber,
                                     @RequestParam("username") String username,
                                     @RequestParam("password") String password) throws Exception {
        return authHandler.register(username, password, phoneNumber, Register.PHONE);
    }

    /**
     * 邮箱注册接口
     *
     * @param email    邮箱
     * @param username 用户名
     * @param password 密码
     * @return 注册结果
     */
    @PostMapping("/register/email")
    public BaseVO<?> registerByEmail(@RequestParam("email") String email,
                                     @RequestParam("username") String username,
                                     @RequestParam("password") String password) throws Exception {
        return authHandler.register(username, password, email, Register.EMAIL);
    }
}